package com.hansming.bss.modules.sfc.wxserve;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

public class WxserveUtil {
	
	public static void  sendServe(String openid,String ACCESS_TOKEN){
		try{
			String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=asdqwe";
			JSONObject jsonobject = new JSONObject();
			JSONObject content = new JSONObject();
			jsonobject.put("touser",openid );
			jsonobject.put("msgtype","text" );
			content.put("content","你好！！！！！！！！！！！！！！！！！！！！！！！！！！！！" );
			jsonobject.put("text",content );
			 post(url,jsonobject);			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public static void post(String URL,JSONObject json) throws IOException, JSONException {
		 
			URL object = new URL(URL);
	        HttpURLConnection conn;
	        conn = (HttpURLConnection) object.openConnection();
	        conn.setDoOutput(true);
	        conn.setDoInput(true);
	        conn.setRequestProperty("Content-Type", "application/json");
	        conn.setRequestProperty("Accept", "application/json");
	        conn.setRequestMethod("POST");
	        conn.setConnectTimeout(5000);
	        conn.setReadTimeout(5000);
	        
	        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
	        wr.write(json.toString());
	        wr.flush();
	        
	        

	        int httpRspCode = conn.getResponseCode();
	        
	      	System.out.println("返回的返回码是："+httpRspCode);
	        
	        
	       
    }
	
public static String getStringInputStream(HttpServletRequest request){
		
		InputStreamReader reader = null;

		BufferedReader breader = null;

		StringBuffer strb = new StringBuffer();

		try {

			reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			breader = new BufferedReader(reader);

			String str = null;

			while (null != (str = breader.readLine())) {

				strb.append(str);

			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		if (null != reader) {

			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		if (null != breader) {

			try {
				breader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return strb.toString();
		
	}
}
